package liurong.stripe.auth;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.model.Card;
import com.stripe.model.Customer;
import com.stripe.model.CustomerCollection;
import com.stripe.model.Token;

/**
 * https://dashboard.stripe.com/test/dashboard
 * 
 * @author liurong
 *
 * @date 2018年9月18日
 */
public class Auth {
	static {
		Stripe.apiKey = "sk_test_A0SBhqWvoyVg3KqshkfDTI3L";
	}

	public static String createCustomer() {
		Map<String, Object> customerParams = new HashMap<String, Object>();
		customerParams.put("description", "from uc");
		customerParams.put("email", "liurong@example.com");
		customerParams.put("source", bindCardCreateToken());

		// cus_BDn9Rn3ZPMUw1H
		Customer create = null;
		try {
			create = Customer.create(customerParams);
			Card card = (Card) create.getSources().getData().get(0);
			System.out.println("默认资源为:" + JSON.toJSONString(create.getDefaultSourceObject(), true));
			System.err.println(JSON.toJSONString(create, true));

		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (CardException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return create == null ? null : create.getId();
	}

	/**
	 * 查询消费者信息
	 */
	public static void queryCustomers() {

		Map<String, Object> customerParams = new HashMap<String, Object>();
		customerParams.put("limit", 1);

		try {
			CustomerCollection list = Customer.list(customerParams);
			System.out.println(JSON.toJSONString(list, true));
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (CardException e) {
			e.printStackTrace();
		} catch (APIException e) {
			e.printStackTrace();
		}
	}

	public static String bindCardCreateToken() {
		Stripe.apiKey = "sk_test_A0SBhqWvoyVg3KqshkfDTI3L";

		Map<String, Object> tokenParams = new HashMap<String, Object>();
		Map<String, Object> cardParams = new HashMap<String, Object>();
		// cardParams.put("number", "4242424242424242");
		cardParams.put("number", "3434343434343434");
		cardParams.put("exp_month", 8);
		cardParams.put("exp_year", 2018);
		cardParams.put("cvc", "314");
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("user_id", "123456");
		cardParams.put("metadata", hashMap);
		tokenParams.put("card", cardParams);

		try {
			Token create = Token.create(tokenParams);
			System.out.println(
					JSON.toJSONString(create, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat));
			return create.getId();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (CardException e) {
			e.printStackTrace();
		} catch (APIException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void queryCardByTokenId() throws Exception {
		Token result = Token.retrieve("tok_1AwLxuEgBzG2TfJ4fbKy51MA");
		System.out.println(JSON.toJSONString(result, SerializerFeature.PrettyFormat));
	}

	public static void main(String[] args) throws Exception {
		// queryCustomers();
		bindCardCreateToken();
		// createCustomer();
		// queryCardByTokenId();
	}
}
